library(pdftools)
library(multipanelfigure)
library(magick)
library(gridExtra)
library(grid)
library(ggplot2)
library(cowplot)
library(ggthemes)
library(gtrendsR)

#get new front pages
mashriq10 <- pdf_render_page("data/newspapers/al-Mashriq News/Jun10.pdf",
                            page = 12)
mashriq10 <- image_read(mashriq10)
mashriq10 <- image_convert(mashriq10, type = "grayscale")
grob0 <- rasterGrob(mashriq10)

mada10 <- pdf_render_page("data/newspapers/al-Mada News/Issue 491.pdf",
                         page = 1)
mada10 <- image_read(mada10)
mada10 <- image_convert(mada10, type = "grayscale")
grob1 <- rasterGrob(mada10)

zaman9 <-  pdf_render_page("data/newspapers/al-Zaman/Jun9P1.pdf",
                           page = 1)
zaman9 <- image_read(zaman9)
zaman9 <- image_convert(zaman9, type = "grayscale")
grob2 <- rasterGrob(zaman9)

g <- grid.arrange(grob0, grob1, grob2, ncol=3)

mashriq11 <- pdf_render_page("data/newspapers/al-Mashriq News/Jun11.pdf",
                             page = 12)
mashriq11 <- image_read(mashriq11)
mashriq11 <- image_convert(mashriq11, type = "grayscale")
grob3 <- rasterGrob(mashriq11)

mada11 <- pdf_render_page("data/newspapers/al-Mada News/Issue 492.pdf",
                          page = 1)
mada11 <- image_read(mada11)
mada11 <- image_convert(mada11, type = "grayscale")
grob4 <- rasterGrob(mada11)

zaman11 <-  pdf_render_page("data/newspapers/al-Zaman/Jun11P1.pdf",
                            page = 1)
zaman11 <- image_read(zaman11)
zaman11 <- image_convert(zaman11, type = "grayscale")
grob5 <- rasterGrob(zaman11)

g1 <- grid.arrange(grob3, grob4, grob5, ncol=3)

grid.arrange(g, g1, ncol=1)

###Figure A.1###
plot_grid(g, g1, labels = "AUTO", nrow = 2)


# get article counts and Google Trends data
counts <- read.csv("data/newspapers/article_counts.csv")

counts$date <- as.Date(counts$date, "%d/%m/%Y")

counts$datenum <- as.numeric(counts$date)
counts$group <- NA
counts$group[counts$datenum<=16231] <- 1
counts$group[counts$datenum>16231] <- 2

counts1 <- counts %>%
  filter(group == 1)

counts2 <- counts %>%
  filter(group == 2)

gtitle1 <- expression(paste(italic("al-Mada"),  "Mosul/ISIS articles, May 28-June 10"))

pg2 <- ggplot(counts1, aes(date, mosul_count)) +
  geom_bar(stat = "identity") +
  ggtitle(gtitle1) +
  ylim(0, 30) +
  xlab("Date") + ylab("Article counts") +
  theme_tufte() +
  theme(aspect.ratio = 1,
        plot.title = element_text(size=20),
        axis.text = element_text(size=20),
        axis.title = element_text(size=20))

gtitle2 <- expression(paste(italic("al-Mada"),  "Mosul/ISIS articles, June 11-June22"))

pg3 <- ggplot(counts2, aes(date, mosul_count)) +
  geom_bar(stat = "identity") +
  ggtitle(gtitle2) +
  ylim(0, 30) +
  xlab("Date") + ylab("Article counts") +
  theme_tufte() +
  theme(aspect.ratio = 1,
        plot.title = element_text(size=20),
        axis.text = element_text(size=20),
        axis.title = element_text(size=20))

grid.arrange(pg2, pg3, nrow=1)

pg <- ggplot(counts, aes(date, mosul_count, fill = date=="2014-06-10")) +
  geom_bar(stat = "identity") +
  geom_vline(xintercept=as.numeric(as.Date("2014-06-10")), col="black", linetype = "dashed")  +
  ggtitle(gtitle2) +
  ylim(0, 30) +
  xlab("Date") + ylab("Article counts") +
  theme_tufte(base_family = "Helvetica") +
  theme(plot.title = element_text(size=20),
        axis.text = element_text(size=20),
        axis.title = element_text(size=20),
        legend.position = "none", strip.background = element_blank(),
        strip.text.x = element_blank()) +
  scale_fill_manual(values = c("grey50", "black"))

plot_grid(pg, nrow=1, labels = "AUTO")

# get Google Trends data
dsh <- gtrends(c("داعش"), 
               time = "2014-05-28 2014-06-22", 
               gprop = "web",
               hl = "ar",
               geo = c("IQ"))

dshdf <- as.data.frame(dsh$interest_over_time)
dshdf$datenum <- as.numeric(dshdf$date)

p1 <- ggplot(dshdf, aes(date, hits)) +
  geom_line(size = 0.5) +
  ggtitle("ISIS") +
  geom_vline(xintercept= 1402354800, col = "black", linetype = "dashed") +
  ylim(0, 100) +
  xlab("Date") + ylab("Search interest") +
  theme_tufte(base_family = "Helvetica") +
  theme(plot.title = element_text(size=20),
        axis.text = element_text(size=20),
        axis.title = element_text(size=20))

mos <- gtrends(c("الموصل"), 
               time = "2014-05-28 2014-06-22", 
               gprop = "web",
               hl = "ar",
               geo = c("IQ"))

mosdf <- as.data.frame(mos$interest_over_time)
mosdf$datenum <- as.numeric(mosdf$date)

p2 <- ggplot(mosdf, aes(date, hits)) +
  geom_line(size = 0.5) +
  ggtitle("Mosul") +
  geom_vline(xintercept= 1402354800, col = "black", linetype = "dashed") +
  ylim(0, 100) +
  xlab("Date") + ylab("Search interest") +
  theme_tufte(base_family = "Helvetica") +
  theme(plot.title = element_text(size=20),
        axis.text = element_text(size=20),
        axis.title = element_text(size=20))

dsh <- gtrends(c("داعش"), 
               time = "2014-05-28 2014-06-22", 
               gprop = "news",
               hl = "ar",
               geo = c("IQ"))

dshdf <- as.data.frame(dsh$interest_over_time)
dshdf$datenum <- as.numeric(dshdf$date)

p3 <- ggplot(dshdf, aes(date, hits)) +
  geom_line(size = 0.5) +
  ggtitle("ISIS") +
  geom_vline(xintercept= 1402354800, col = "black", linetype = "dashed") +
  ylim(0, 100) +
  xlab("Date") + ylab("News interest") +
  theme_tufte(base_family = "Helvetica") +
  theme(plot.title = element_text(size=20),
        axis.text = element_text(size=20),
        axis.title = element_text(size=20))

mos <- gtrends(c("الموصل"), 
               time = "2014-05-28 2014-06-22", 
               gprop = "news",
               hl = "ar",
               geo = c("IQ"))

mosdf <- as.data.frame(mos$interest_over_time)
mosdf$datenum <- as.numeric(mosdf$date)

p4 <- ggplot(mosdf, aes(date, hits)) +
  geom_line(size = 0.5) +
  ggtitle("Mosul") +
  geom_vline(xintercept= 1402354800, col = "black", linetype = "dashed") +
  ylim(0, 100) +
  xlab("Date") + ylab("News interest") +
  theme_tufte(base_family = "Helvetica") +
  theme(plot.title = element_text(size=20),
        axis.text = element_text(size=20),
        axis.title = element_text(size=20))

pg4 <- grid.arrange(p1, p2, nrow=1)
pg5 <- grid.arrange(p3, p4, nrow=1)

plot_grid(pg4, pg5, pg, nrow=3, labels = "AUTO")
plot_grid(pg4, pg5, nrow=2, labels = c("B", "C"))

###Figure 1###
png("plots/gtrendsmada.png", width = 465, height = 225, units='mm', res = 300)
plot_grid(pg4, pg5, pg, nrow=3, labels = "AUTO")
dev.off()

# plot cartoons

c <- pdf_render_page("data/newspapers/cartoon.pdf")
c <- image_read(c)
c<- image_convert(c, type = "grayscale")
grob0 <- rasterGrob(c)

c1 <- pdf_render_page("data/newspapers/cartoon1.pdf")
c1 <- image_read(c1)
c1<- image_convert(c1, type = "grayscale")
grob1 <- rasterGrob(c1)

c2 <- pdf_render_page("data/newspapers/cartoon2.pdf")
c2 <- image_read(c2)
c2<- image_convert(c2, type = "grayscale")
grob2 <- rasterGrob(c2)

plot_grid(grob0, grob1, labels = "AUTO", nrow = 2)

g <- grid.arrange(grob1, grob2, ncol=2)

plot_grid(grob0, g, labels = "AUTO", nrow = 2)

###Figure 2###
png("plots/cartoons.png", width = 170, height = 130, units='mm', res = 300)
plot_grid(grob0, g, labels = "AUTO", nrow = 2)
dev.off()
